package com.cgtong.android.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.mvp.contract.ForgetPasswordContract;
import com.cgtong.android.mvp.presenter.ForgetPasswordPresenter;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.ValidCodeTime;
import com.kingja.loadsir.callback.SuccessCallback;
import com.umeng.analytics.MobclickAgent;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Sky.Bing on 2017/10/20.
 */

public class ForgetPasswordActivity extends BaseTitleActivity implements ForgetPasswordContract.View {
    @BindView(R.id.et_phone)
    public EditText mEtPhone;
    @BindView(R.id.et_validcode)
    public EditText mEtValidCode;
    @BindView(R.id.et_new_password)
    public EditText mEtNewPassword;
    @BindView(R.id.et_new_confirm_password)
    public EditText mEtNewConfirmPassword;

    @BindView(R.id.btn_send_validcode)
    public Button mBtnSendValidCode;

    private ValidCodeTime validCodeTime;
    private ForgetPasswordPresenter mForgetPasswordPresenter;

    @Override
    protected String getContentTitle() {
        return getResources().getString(R.string.txt_forget_password);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void loadDataByNet() {
        validCodeTime = new ValidCodeTime(60000, 1000);
        validCodeTime.setTimeSetBtn(mBtnSendValidCode);
        mForgetPasswordPresenter = new ForgetPasswordPresenter(this);
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
    }

    @Override
    public void breakClickValidCode() {
        ToastUtils.showLong(R.string.txt_success_send_valid_code);
        validCodeTime.start();
    }

    @Override
    protected void onDataReloadByNet(View v) {

    }

    @OnClick(R.id.btn_send_validcode)
    public void sendValidCode() {
        String phone = mEtPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showLong(R.string.txt_empty_phone);
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            ToastUtils.showLong(R.string.txt_phone_format_error);
            return;
        }
        mForgetPasswordPresenter.sendValidCode(phone);
    }

    @OnClick(R.id.btn_modify_password)
    public void doModifyPassword() {
        String phone = mEtPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showLong(R.string.txt_empty_phone);
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            ToastUtils.showLong(R.string.txt_phone_format_error);
            return;
        }
        String validCode = mEtValidCode.getText().toString();
        if (TextUtils.isEmpty(validCode)) {
            ToastUtils.showLong(R.string.txt_empty_validcode);
            return;
        }
        String newPassword = mEtNewPassword.getText().toString();
        if (TextUtils.isEmpty(newPassword)) {
            ToastUtils.showLong(R.string.txt_empty_password);
            return;
        }
        if(CommonUtils.containsEmoji(newPassword)){
            ToastUtils.showLong(R.string.txt_password_nocontains_emoji);
            return;
        }
        if (!CommonUtils.checkPasswordRule(newPassword)) {
            ToastUtils.showLong(R.string.txt_irregular_password);
            return;
        }
        String newConfirmPassword = mEtNewConfirmPassword.getText().toString();
        if (TextUtils.isEmpty(newConfirmPassword)) {
            ToastUtils.showLong(R.string.txt_empty_password_again);
            return;
        }
        if (!newPassword.equals(newConfirmPassword)) {
            ToastUtils.showLong(R.string.txt_diff_two_password);
            return;
        }
        mForgetPasswordPresenter.saveForgetPwd(newConfirmPassword, phone, newPassword, validCode);
    }

    @Override
    public void onSaveForgetPwdError(ApiException e) {
        if (!TextUtils.isEmpty(e.getMessage())) {
            ToastUtils.showLong(e.getMessage());
        } else {
            ToastUtils.showLong(R.string.txt_save_forgetpwd_error);
        }
    }

    @Override
    public void onSaveForgetPwdSuccess() {
        ToastUtils.showLong(R.string.txt_save_forgetpwd_success);
        finish();
    }

    @Override
    public void onSendValidCodeError(ApiException e) {
        if (!TextUtils.isEmpty(e.getMessage())) {
            ToastUtils.showLong(e.getMessage());
        } else {
            ToastUtils.showLong(R.string.txt_sendvalidcode_error);
        }
    }
}